package com.magic.shop.util;


import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

//import org.apache.poi.util.IOUtils;

/**
 * Created by IDEA.
 * User: jiangXueZhi
 * Date: 2021/9/27
 * Time: 3:40 下午
 */
@Slf4j
public class FileUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);


    public static void delete(File folder) {
        if(folder.isDirectory()) {
            File[] files = folder.listFiles();
            if(files != null) { // 如果文件夹为空，files可能为null
                for(File file : files) {
                    if(file.isFile()){
                        file.delete();
                    }else{
                        delete(file); // 递归删除子文件夹和文件
                    }

                }
            }
        }
        folder.delete(); // 删除空文件夹或者文件
    }
    /**
     * @param file     文件
     * @param fileName 新的随机文件名
     */
    public static File upload(MultipartFile file, String destPath, String fileName) {
        File dest = new File(destPath + File.separator + fileName);
        //判断文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            //保存文件
            file.transferTo(dest);
        } catch (Exception e) {
            log.info("Save file exception. {}", e.getMessage());
        }
        return dest;
    }

    /**
     * 输出文件的后缀名
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName) {
//        fileName.substring(fileName.lastIndexOf("."))
//        是C#中的字符串操作，它的作用是从文件名字符串中截取最后一个点（.）及其后面的所有字符。这个操作可以用来获取文件的扩展名。
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /**
     * 输出文件的新名称 (名字+后缀名)
     * @param fileName
     * @return
     */
    public static String generateRandomFileName(String fileName) {
        return UUID.randomUUID() + getSuffix(fileName);
    }

    /**
     * @param name
     * @Description 设置响应头部信息
     * @Throws
     * @Return java.lang.String
     * @Date 2023-08-02 13:39:15
     * @Author lgm
     */
    public static String fileContentType(String name) {
        String result = "";
        String fileType = name.toLowerCase();
        if (fileType.endsWith(".png")) {
            result = "image/png";
        } else if (fileType.endsWith(".gif")) {
            result = "image/gif";
        } else if (fileType.endsWith(".jpg") || fileType.endsWith(".jpeg")) {
            result = "image/jpeg";
        } else if (fileType.endsWith(".svg")) {
            result = "image/svg+xml";
        } else if (fileType.endsWith(".doc")) {
            result = "application/msword";
        } else if (fileType.endsWith(".xls")) {
            result = "application/x-excel";
        } else if (fileType.endsWith(".zip")) {
            result = "application/zip";
        } else if (fileType.endsWith(".pdf")) {
            result = "application/pdf";
        } else if (fileType.endsWith(".mpeg")) { //MP3
            result = "audio/mpeg";
        } else if (fileType.endsWith(".mp4")) {
            result = "video/mp4";
        } else if (fileType.endsWith(".plain")) {
            result = "text/plain";
        } else if (fileType.endsWith(".html")) {
            result = "text/html";
        } else if (fileType.endsWith(".json")) {
            result = "application/json";
        } else {
            result = "application/octet-stream";
        }
        return result;

    }
}
